package cn.wolfcode.trip.app.web.interceptor;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//登录拦截器
//用于拦截没有登录用户访问的资源
public class CheckLoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //获取当前的用户对象

        User currentUser = UserContext.getCurrentUser();
        //如果当前用户没有登录跳转到指定页面
        if (currentUser == null) {
            response.sendRedirect("/login.html");
            return false;
        }
        return true;
    }
}
